/* Filename: username.c
 * Description: 输入uid打印username
 * Last modified: humble 20200315 12:31
 */
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <pwd.h>

static void usage(void);

int main(int argc, char **argv) {
    if(argc != 2){ fprintf(stderr, "argc !=2"); usage(); exit(1); }

    // struct passwd *getpwuid(uid_t uid) 用于根据用户 ID（UID）查找对应的用户信息
    uid_t uid = atoi(argv[1]);
    struct passwd *passwdres = getpwuid(uid);
    if(passwdres == NULL){ fprintf(stderr, "User with UID %d not found.\n", uid); usage(); exit(1); }
    
    printf("name:%s\n", passwdres->pw_name);
    return 0;
}

static void usage(void) {
#define USAGE "Usage:\n\
  Helloworld\n"
    printf(USAGE);
}
